package 手写;

import java.util.concurrent.atomic.AtomicInteger;

public class 三个线程交替打印0到100 {
    public static void main(String[] args) {
        final AtomicInteger i = new AtomicInteger(0);
        final int n = 100;

        Thread t1 = new Thread(() -> {
            while (true) {
                int k = i.get();
                if (k > n) {
                    break;
                }
                if (k % 3 == 0) {
                    System.out.println("t1---->" + k);
                    i.getAndIncrement();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            while (true) {
                int k = i.get();
                if (k > n) {
                    break;
                }
                if (k % 3 == 1) {
                    System.out.println("t2---->" + k);
                    i.getAndIncrement();
                }
            }
        });

        Thread t3 = new Thread(() -> {
            while (true) {
                int k = i.get();
                if (k > n) {
                    break;
                }
                if (k % 3 == 2) {
                    System.out.println("t3---->" + k);
                    i.getAndIncrement();
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}
